package com.taogewan.af;

import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.appcompat.app.AppCompatActivity;

import io.flutter.embedding.android.FlutterFragment;
import io.flutter.embedding.android.FlutterView;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterEngineCache;
import io.flutter.embedding.engine.dart.DartExecutor;

public class FlutterPageActivity extends AppCompatActivity {
    private FlutterEngine flutterEngine;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flutter_page);
        // 通过FlutterView引入Flutter编写的页面
//        View flutterView = Flutter.createView(this, getLifecycle(), "route1");
//        FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams(600, 800);
//        layout.leftMargin = 100;
//        layout.topMargin = 200;
//        addContentView(flutterView, layout);

//        // 通过FlutterView引入Flutter编写的页面
//        FlutterView flutterView = new FlutterView(this);
//        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
//                ViewGroup.LayoutParams.MATCH_PARENT,
//                ViewGroup.LayoutParams.MATCH_PARENT);
//        FrameLayout flContainer = findViewById(R.id.fl_container);
//        flContainer.addView(flutterView, lp);
//
////        // 关键代码，将Flutter页面显示到FlutterView中
////        flutterView.attachToFlutterEngine(flutterEngine);
////
////        flutterEngine = new FlutterEngine(this);
////        flutterEngine.getDartExecutor().executeDartEntrypoint(
////                DartExecutor.DartEntrypoint.createDefault()
////        );
////        flutterEngine.getNavigationChannel().setInitialRoute("route1");
//
//        // 创建可缓存的FlutterEngine对象
//        FlutterEngine flutterEngine = new FlutterEngine(this);
//        flutterEngine.getNavigationChannel().setInitialRoute("route1");
//        flutterEngine.getDartExecutor().executeDartEntrypoint(
//                DartExecutor.DartEntrypoint.createDefault()
//        );
//        FlutterEngineCache.getInstance().put("my_engine_id", flutterEngine);

        // 通过FlutterFragment引入Flutter编写的页面 https://www.jianshu.com/p/7b6522e3e8f1
        FlutterFragment flutterFragment = FlutterFragment.withNewEngine()
                .initialRoute("route1")
                .build();
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.fl_container, flutterFragment)
                .commit();
    }

    @Override
    protected void onResume() {
        super.onResume();
//        flutterEngine.getLifecycleChannel().appIsResumed();
    }

    @Override
    protected void onPause() {
        super.onPause();
//        flutterEngine.getLifecycleChannel().appIsInactive();
    }

    @Override
    protected void onStop() {
        super.onStop();
//        flutterEngine.getLifecycleChannel().appIsPaused();
    }
}
